﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Search.Model;
@using CNKI.TPI.Web.Base;
<script type="text/javascript">

    //删除样式
    function delStyle() {
        ;
        var len = $('#tableField tbody tr').size();
        if (len > 0) {
            confirmDialog('确定要删除吗?', function () {
                $.ajax({
                    type: "POST",
                    url: "@Html.Raw(Url.Action("DelRelation", "Admin_SingleDBPublish"))",
                    data: { "dbcode": "@ViewBag.DbCode" },
                    success: function (m) {
                        $("#tableField tbody").empty();
                        $(".wrapR-c-box-b-right ul").empty();
                    },
                    error: function (e) {
                        messageAlert("操作失败！");
                    }
                })
            })
        }
        else {
            messageAlert("列表暂无数据，不支持删除！");
        }
    }
    //创建样式
    function createStyle() {
        var len = $(".wrapR-c-box-b-right ul li label").size();
        if (len > 0) {
            delOldStyle();
            //循环选择的字段
            $(".wrapR-c-box-b-right ul li label").each(function () {
                ;
                var dbname = $.trim($(this).text());
                var trnew = existField(dbname);
                var dbcode = $(this).find("input").val();
                var selectedvar = "";
                if (trnew == "") {
                    //插入新记录
                    //获取数据库字段列表
                    var dbfieldlist = "";
                    
                    $.ajax({
                        type: "POST",
                        async: false,
                        url: "@Html.Raw(Url.Action("GetFieldString", "Admin_SingleDBPublish"))",
                        data: { "dbcode": dbcode },
                        success: function (data) {
                            var obj = eval(data)[0];
                            dbfieldlist = obj.msg;
                            if (obj.vsmselect == 1) {
                                selectedvar = "disabled=disabled";
                            }
                        }
                    })

                    trnew = "<tr><td class=\"tc\"><label><input type=\"hidden\"  value=\"" + dbcode + "\"/>" + dbname + "</label></td>" +
                    "<td class=\"tc\"> <select class=\"f-select span2\"  onchange=\"tableselectchange(this)\">" + dbfieldlist + "</select></td>" +
                    "<td class=\"tc\"> <select class=\"f-select span2\"" + selectedvar + ">" + $("#ThisOptionSelect").val() + "</select></td>" +
                    "<td class=\"tc\"> <input type=\"text\" value=\"" + dbname + "\" class=\"f-text span3\" maxlength=\"20\"> </td></tr>"
                }
                $("#tableField tbody").append(trnew);
            })
            messageAlert("生成完成！");
        }
        else {
            messageAlert("请先选择提交字段！");
        }
    }
    //判断数据库是否存在，存在则返回已有tr，且从table移除，不存在则返回空
    function existField(fieldtext) {
        var trtext = "";
        $("#tableField tbody tr").each(function () {
            var thistr = $(this);
            if ($.trim(thistr.find("td").eq(0).text()) == $.trim(fieldtext)) {
                trtext = $(thistr);
                thistr.remove();
                return trtext;
            }
        })
        return trtext;
    }
    //先删除table里面与右侧列表中不匹配的数据库
    function delOldStyle() {
        $("#tableField tbody tr").each(function () {
            var thistr = $(this);//行
            var m = 0;
            $(".wrapR-c-box-b-right ul li label").each(function () {
                var thistext = $.trim($(this).text());
                if ($.trim(thistr.find("td").eq(0).text()) == $.trim(thistext)) {
                    m = 1;
                }
            })
            if (m == 0) {
                thistr.remove();
            }
        })
    }
    //保存
    function saveFieldList() {
        ;
        disablesave();
        var len = $('#tableField tbody tr').size();
        if (len <= 0) {
            messageAlert("请先生成提交样式！");
            enablesave();
            return;
        }
        //循环table保存
        var jsonfield = [];
        var flage = true;
        $('#tableField tbody tr').each(function () {
            var tdobj = $(this).find("td");
            var defalutval = $.trim(tdobj.eq(3).find("input").val());
            if (defalutval != "") {
                if (!regtext(defalutval)) {
                    flage = false;
                }
            }
            var data = {
                "DBCODE": "@ViewBag.DbCode",
                "DBField": tdobj.eq(2).find("select").val(),
                "RelationDBCode": $.trim(tdobj.eq(0).find("input").val()),
                "RelationDBField": tdobj.eq(1).find("select").val(),
                "Status": 1,
                "SortNo": len,
                "Title": $.trim(tdobj.eq(3).find("input").val()),
            }
            len = len - 1;
            jsonfield.push(data);
        })
        var datastr = { "list": jsonfield };
        if (flage) {
            $.ajax({
                type: "POST",
                url: "@Html.Raw(Url.Action("SaveRelation", "Admin_SingleDBPublish"))",
                contentType: "application/json",
                data: JSON.stringify(datastr),
                success: function (m) {
                    if (m == "True") {
                        messageAlert("操作成功！");
                    }
                    else {
                        messageAlert("操作失败！");
                    }
                    enablesave();
                },
                error: function (e) {
                    messageAlert("操作失败！");
                    enablesave();
                }
            })
        }
        else {
            messageAlert("请检查输入项是否有特殊字符！");
            enablesave();

        }
        enablesave();
    }
    function disablesave() {
        $(".btn-warn-f").attr("disabled", "disabled");
    }
    function enablesave() {
        $(".btn-warn-f").removeAttr("disabled", "disabled");
    }
    //文本框验证特殊字符
    function onBlurText(e) {
        var value = $(e).val();
        if (value != "") {
            if (!regtext(value)) {
                messageAlert("不允许输入特殊字符！");
            }
        }
    }
    //下拉列表变化事件
    function tableselectchange(e) {
        ;
        var trobj = e.parentNode.parentNode;
        var selvalue = $(e).val();
        if (selvalue == "SYS_FLD_VSM") {
            $(trobj).find("td").eq(2).find("select").val("SYS_FLD_VSM");
            $(trobj).find("td").eq(2).find("select").attr("disabled", "disabled");
        }
        else {
            $(trobj).find("td").eq(2).find("select").find("option:first").prop("selected", 'selected');
            $(trobj).find("td").eq(2).find("select").removeAttr("disabled", "disabled");


        }
    }
    //下拉列表变化事件
    //function thisselectchange(e) {
    //    ;
    //    var trobj = e.parentNode.parentNode;
    //    var selvalue = $(e).val();
    //    if (selvalue == "SYS_FLD_VSM") {
    //        $(trobj).find("td").eq(1).find("select").val("SYS_FLD_VSM");
    //        $(trobj).find("td").eq(1).find("select").attr("disabled", "disabled");
    //    }
    //    else {
    //        $(trobj).find("td").eq(1).find("select").removeAttr("disabled", "disabled");
    //    }
    //}
</script>
<!-- wrapRStart -->
<h2>数据库：@ViewBag.DbName</h2>
<h3 class='box-h'>关联库字段设置</h3>
<div class="wrapR-c wrapR-c2" style="margin-top: 20px">
    <div class="clearfix posr wrapR-c2-in">
        <div class="wrapR-c-box wrapR-c-l fl posr" style="overflow-y: visible">
            <div class="wrapR-c-box-t">待选数据库</div>
            <div class="wrapR-c-box-b-left" style="overflow-y: scroll; height: 380px">
                <ul>

                    @if (ViewBag.Dblist != null)
                    {
                        foreach (SYS_CMS_DATABASE item in ViewBag.Dblist)
                        {
                          
                        <li class="f-ct">
                            <label class="f-label block">
                                <input type="checkbox" class="f-checkboxL" onclick="leftcheck(this)" value='@item.DatabaseCode' />@item.DatabaseName</label>

                        </li>
                  
                        }
                    }
                </ul>
                <div class="f-ct wrapR-c-box-footer posa">
                    <label class="f-label ml10">
                        <input type="checkbox" class="f-checkbox-left" onclick="leftCheckAll(this)" />全选</label>
                </div>
            </div>

        </div>
        <div class="wrapR-c-m tc posa"><a class="iconfont-s iconfont-right-triangle-s" onclick="rightMove('.wrapR-c-box-b-left','.wrapR-c-box-b-right')"></a></div>
        <div class="fr posr">
            <div class='wrapR-c-box wrapR-c-r wrapR-c-r-l' style="overflow-y: visible">
                <div class="wrapR-c-box-t">相似关联数据库</div>
                <div class="wrapR-c-box-b-right" style="overflow-y: scroll; height: 380px">
                    <ul class="ulfield">
                        @if (ViewBag.RelationDbList != null)//修改时加载已经设置
                        {
                            foreach (SYS_CMS_DATABASE_RELATION item in ViewBag.RelationDbList)
                            {
                            <li class="f-ct">
                                <label class="f-label block right">
                                    <input type="checkbox" class="f-checkboxR" onclick="inputclickright(this)" value='@item.RelationDBCode' />@item.RelationDBName</label>

                            </li>
                            }
                        }
                    </ul>
                    <div class="f-ct wrapR-c-box-footer posa clearfix">
                        <label class="f-label ml10 fl">
                            <input type="checkbox" class="f-checkbox-right" onclick="rightCheckAll(this)">全选</label>
                        <a class="btn btn-s btn-warn-f fr deleteright" onclick="deleteright()">删除</a>
                    </div>
                </div>
            </div>

            <div class="wrapR-c-r-r form-h posa" style="top: 10px">
                <a title="移至最顶部" onclick=" movefirst(this)" class="iconfont-s iconfont-upest-s"></a>
                <br />
                <a title="上移" onclick="moveablove(this)" class="iconfont-s iconfont-up-s"></a>
                <br />
                <a title="下移" onclick="movedown(this)" class="iconfont-s iconfont-down-s"></a>
                <br />
                <a title="移至最底部" onclick="movelast(this)" class="iconfont-s iconfont-downest-s"></a>

            </div>
            <div class="posa" style="bottom: 0; right: -105px;">
                <a onclick="createStyle()" class="btn btn-m btn-prim-f mb10">生成设置</a><br />
                <a onclick="delStyle();" class="btn btn-m btn-defa-f">删除设置</a>
            </div>
        </div>


    </div>
    <table id="tableField" width="100%" cellspacing="0" cellpadding="0" class="table table-wr" style="margin-top: 20px;">
        <thead>
            <tr class="thead">
                <th>待关联库</th>
                <th>选择 待关联库 字段</th>
                <th>选择 @ViewBag.DbName 字段</th>
                <th>前台显示标题</th>
            </tr>
        </thead>
        <tbody>
            @if (ViewBag.RelationDblist != null)//修改时加载已经设置的字段
            {
                foreach (SYS_CMS_DATABASE_RELATION item in ViewBag.RelationDblist)
                {
                <tr>
                    <td class="tc">
                        <label>
                            <input type="hidden"  value="@item.RelationDBCode"/></label>@item.RelationDBName</td>
                    <td class="tc">
                        <select class="f-select span2" onchange="tableselectchange(this)">
                            @if (item.RelationDBFieldList != null)
                            {
                                foreach (SYS_CMS_DATABASE_FIELD selField in item.RelationDBFieldList)
                                {
                                    if (item.RelationDBField == selField.Field)
                                    {
                                <option value=" @selField.Field" selected="selected">@selField.Field</option>
                                    }
                                    else
                                    {
                                <option value="@selField.Field">@selField.Field</option>
                                    }

                                }
                            }
                        </select>
                    </td>
                    <td class="tc">
                        @{
                            var dis = "";
                            if (item.DBField == "SYS_FLD_VSM")
                            {
                                dis = "disabled=disabled";
                            }
                            else
                            {
                                dis = "";
                            }
                            <select class="f-select span2" @dis>
                                @if (ViewBag.ThisDbFieldList != null)
                                {
                                    foreach (SYS_CMS_DATABASE_FIELD sel in ViewBag.ThisDbFieldList)
                                    {
                                        if (sel.Field == "SYS_FLD_VSM")
                                        {
                                            dis = "disabled=disabled";
                                        }
                                        else
                                        {
                                            dis = "";
                                        }
                                        if (item.DBField == sel.Field)
                                        {
                                    <option value=" @sel.Field" @dis selected="selected">@sel.Field</option>
                                        }
                                        else
                                        {
                                    <option value="@sel.Field" @dis>@sel.Field</option>
                                        }

                                    }
                                }
                            </select>
                        }
                    </td>
                    <td class="tc">
                        <input type="text" value="@item.Title" onblur="onBlurText(this)" class="f-text span3"  maxlength="20">
                    </td>
                </tr>
                }
            }
        </tbody>
    </table>

    <div class="tc mt30" style="padding-bottom: 30px">
        <button class="btn btn-m btn-submit btn-warn-f" onclick="saveFieldList()">保存</button>
        <a class="btn btn-m btn-prim-f" href="javascript:void(0);" onclick="ajaxLoad('initload','@Html.Raw(Url.Action("SmilarIndex", "Admin_SingleDBPublish"))')">返回</a>
    </div>
    <input type="hidden" id="ThisOptionSelect" name="ThisOptionSelect" value="@ViewBag.ThisOptionSelect" />
</div>
<!-- wrapREnd -->


